package com.huike.web.controller.clues;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.dto.ActivityUpdateDTO;
import com.huike.clues.domain.dto.TbActivityAddDTO;
import com.huike.clues.domain.dto.TbActivityPageDto;
import com.huike.clues.domain.vo.ActivityEchoVo;
import com.huike.clues.service.TbActivityService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.result.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author lxc
 * @date 2023/10/15
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/clues/activity")
public class TbActivityController {

    private final TbActivityService tbActivityService;

    @PostMapping
    public AjaxResult addActivity(@RequestBody TbActivityAddDTO tbActivityAddDTO) {
        log.info("启动了新添活动接口{}", tbActivityAddDTO);
        tbActivityService.insertActivity(tbActivityAddDTO);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    public PageResult findActivityList(TbActivityPageDto tbActivityPageDto) {
        // @RequestParam("params[beginCreateTime]") @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate beginCreateTime,
        // @RequestParam("params[endCreateTime]") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime,
        // @RequestParam("params[beginTime]") @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate beginTime,
        // @RequestParam("params[endTime]") @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate endTime) {
        log.info("执行分页查询活动{}", tbActivityPageDto);
        LocalDate beginCreateTime = null;
        LocalDate endCreateTime = null;
        LocalDate beginTime = null;
        LocalDate endTime = null;
        if (tbActivityPageDto.getParams().get("beginCreateTime") != "" &&
                tbActivityPageDto.getParams().get("endCreateTime") != "" &&
                tbActivityPageDto.getParams().get("beginTime") != "" &&
                tbActivityPageDto.getParams().get("endTime") != "") {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String beginCreateTimeStr = (String) tbActivityPageDto.getParams().get("beginCreateTime");
            beginCreateTime = LocalDate.parse(beginCreateTimeStr, formatter);

            String endCreateTimeStr = (String) tbActivityPageDto.getParams().get("endCreateTime");
            endCreateTime = LocalDate.parse(endCreateTimeStr, formatter);

            String beginTimeStr = (String) tbActivityPageDto.getParams().get("beginTime");
            beginTime = LocalDate.parse(beginTimeStr, formatter);

            String endTimeStr = (String) tbActivityPageDto.getParams().get("endTime");
            endTime = LocalDate.parse(endTimeStr, formatter);
        }
        Page<TbActivity> page = tbActivityService.findActivityList(tbActivityPageDto, beginCreateTime, endCreateTime, beginTime, endTime);
        return PageResult.success(page.getTotal(), page.getRecords());
    }

    @GetMapping("/{id}")
    public AjaxResult<ActivityEchoVo> findActivityById(@PathVariable Long id) {
        log.info("执行了根据id查询活动接口Id{}", id);
        ActivityEchoVo activityEchoVo = tbActivityService.findActivityById(id);
        return AjaxResult.success(activityEchoVo);
    }

    @PutMapping
    public AjaxResult updateActivity(@RequestBody ActivityUpdateDTO activityUpdateDTO) {
        log.info("执行修改活动接口{}", activityUpdateDTO);
        tbActivityService.updateActivity(activityUpdateDTO);
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("/{ids}")
    public AjaxResult deleteActivity(@PathVariable List<Long> ids) {
        log.info("执行了根据id删除接口,id{}", ids);
        tbActivityService.deleteActivity(ids);
        return AjaxResult.success("删除成功");
    }

    @GetMapping("/listselect/{channel}")
    public AjaxResult<List<ActivityEchoVo>> activityListSelect(@PathVariable String channel){
        log.info("执行查询渠道来源为{}的活动列表",channel);
        List<ActivityEchoVo> list = tbActivityService.activityListSelect(channel);
        return AjaxResult.success(list);

    }
}
